package com.greyu.ysj.service.impl;


import com.greyu.ysj.config.ResultStatus;
import com.greyu.ysj.entity.Admin;
import com.greyu.ysj.entity.AdminExample;
import com.greyu.ysj.mapper.AdminMapper;
import com.greyu.ysj.model.ResultModel;
import com.greyu.ysj.service.AdminService;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;


@Service
@Transactional
public class AdminServiceImpl implements AdminService {
    @Autowired
    private AdminMapper adminMapper;

	@Override
	public Admin selectByUserName(String username) {
		
		AdminExample adminExample = new AdminExample();
        AdminExample.Criteria criteria = adminExample.createCriteria();
        criteria.andUserNameEqualTo(username);
        List<Admin> users = this.adminMapper.selectByExample(adminExample);

        Admin user;
        try {
            user = users.get(0);
        } catch (Exception e) {
            return null;
        }
        return user;
	}

	@Override
	public Admin selectByUserId(int userId) {
		
		AdminExample adminExample = new AdminExample();
        AdminExample.Criteria criteria = adminExample.createCriteria();
        criteria.andUserIdEqualTo(userId);
        List<Admin> users = this.adminMapper.selectByExample(adminExample);

        Admin user;
        try {
            user = users.get(0);
        } catch (Exception e) {
            return null;
        }
        return user;
	}

	@Override
	public ResultModel addUser(Admin user) {
		System.out.println(user.getUserName());
		System.out.println(user.getPassWord());
		if(user.getUserName()==null||user.getUserName().trim().isEmpty()
				||user.getPassWord()==null||user.getPassWord().trim().isEmpty()) {
			return ResultModel.error(ResultStatus.DATA_NOT_NULL);
		}
		
		Admin newUser=this.selectByUserName(user.getUserName());
		if(newUser!=null) {
			return ResultModel.error(ResultStatus.USER_HAS_EXISTS);
		}
		this.adminMapper.insert(user);
		
		return ResultModel.ok(user);
	}

	@Override
	public Admin selectByPhone(String phone) {
		
		AdminExample adminExample = new AdminExample();
        AdminExample.Criteria criteria = adminExample.createCriteria();
        criteria.andPhoneEqualTo(phone);
        List<Admin> users = this.adminMapper.selectByExample(adminExample);

        Admin user;
        try {
            user = users.get(0);
        } catch (Exception e) {
            return null;
        }
        return user;
	}
    

		
}
